package com.jiantong.news.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.news.model.ThinktankVideo;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 养殖智库视频
 * Created by mz03 on 2017/4/13.
 */
@Repository
public class ThinktankVideoDao extends MyBatisDao<ThinktankVideo> {

    //根据类型查询
    public List<ThinktankVideo> findByType(Integer type){
        return getSqlSession().selectList(sqlId("findByType"),type);
    }

    /**
     * 分页查询
     * @param offset 页码
     * @param limit 条数
     * @param criteria 参数
     * @return Paging<ThinktankVideo>
     */
    public Paging<ThinktankVideo> pagingByKeyword(Integer offset, Integer limit, Map<String, Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = (Long)this.sqlSession.selectOne(this.sqlId("countKeyword"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            (criteria).put("offset", offset);
            (criteria).put("limit", limit);
            List<ThinktankVideo> datas = this.sqlSession.selectList(this.sqlId("pagingKeyword"), criteria);
            return new Paging<>(total, datas);
        }
    }
}
